package com.qj.platform.pay.controller;

import com.qj.base.commons.exception.CodeDefinition;
import com.qj.base.commons.utils.RSA;
import com.qj.base.controller.BaseController;
import com.qj.base.domain.ResultTemplate;
import com.qj.platform.pay.commons.execption.SdsException;
import com.qj.platform.pay.core.domain.User;
import com.qj.platform.pay.core.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hezhijie on 2016/11/17.
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @RequestMapping("/query")
    public ResultTemplate queryList(String id,String name,String userCode,String machineCode){
        return getSuccessTemplate(userService.queryList(id,userCode,name,machineCode,-1,-1));
    }

    @RequestMapping("/login")
    public ResultTemplate login(String userCode,String pwd,String ip,String mac,String machineCode){
        if(StringUtils.isEmpty(userCode)||StringUtils.isEmpty(pwd)){
            throw new SdsException(CodeDefinition.ILLEGAL_ARGUMENT_VALUE,"请填写用户名或者密码");
        }
        if(StringUtils.isEmpty(machineCode)){
            throw new SdsException(CodeDefinition.ILLEGAL_ARGUMENT_VALUE,"未获取到机器码");
        }
        User user=userService.login(userCode, pwd,ip,mac,machineCode);
        Map<String,Object> objectMap=new HashMap<>();
        objectMap.put("user",user);
        objectMap.put("token", RSA.getInstance().encrypt(String.format("USER#%s#%s#%s",user.getUserCode(),machineCode,System.currentTimeMillis())));
        return getSuccessTemplate(objectMap);
    }

    @RequestMapping("/getUserInfo")
    public ResultTemplate getUserInfo(){
        System.out.println(userService.getUserInfo());
        return getSuccessTemplate(null);
    }

}
